/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.giant.bean.transactions;

import com.giant.bean.parametrics.OpCenBean;
import com.giant.entities.InDetOrder;
import com.giant.exceptions.OperacionInvalidaException;
import com.giant.services.IServicioPersistenciaLocal;
import com.giant.services.ServicioPersistencia;
import com.giant.services.transactions.ServiceDetOrderLocal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Harold
 */
@ManagedBean
@SessionScoped
public class DetOrderBean {

    /**
     * Relación con la interfaz que provee los servicios necesarios del vendedor
     */
    @EJB
    private ServiceDetOrderLocal serviceDetOrder;
    //-----------------------------------------------------------
    // Atributos
    //-----------------------------------------------------------
    /**
     * Representa un nuevo vendedor a ingresar
     */
    private InDetOrder detOrder = new InDetOrder();
    
    private String param="";

    private String noOrder="";
    
    private Date dateOrder;
    
    private List<InDetOrder> allDets;
    
    @EJB
    private IServicioPersistenciaLocal persistence;

    /**
     * Creates a new instance of OpCenBean
     */
    public DetOrderBean() {
         persistence = new ServicioPersistencia();
    }

    //-----------------------------------------------------------
    // Getters y setters
    //-----------------------------------------------------------
    /**
     * Devuelve el objeto de vendedor actual
     *
     * @return vendedor Vendedor actual
     */
    public InDetOrder getDetOrder() {
        return detOrder;
    }

    /**
     * Modifica al vendedor actual
     *
     * @param vendedor Nuevo vendedor
     */
    public void setDetOrder(InDetOrder det) {
        this.detOrder = det;
    }

    /**
     * Devuelve todos los vendedores del sistema
     *
     * @return vendedores Lista con todos los vendedores del sistema
     */
    public List<InDetOrder> getDetOrders() {
//           if(allDets==null)
//        {
            String query="SELECT i FROM InDetOrder i WHERE i.idOrder ="+this.noOrder;
            System.out.println(query);
            allDets=persistence.executeObjectSql(query);
//            allDets=serviceDetOrder.getDetOrder();
//        }
          totalDetails();
        return allDets;
    }

    public void criteriaFilter(String sql, String param) {

        Map<String, String> map = new HashMap<String, String>();
        map.put(param, this.param);

        allDets = this.serviceDetOrder.getDetOrderFilter(sql, map);

    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public boolean saveDetOrder() {
        boolean ret = true;
        try {
            serviceDetOrder.agregarDetOrder(detOrder);
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Successful", "Successful save"));

        } catch (OperacionInvalidaException ex) {
            Logger.getLogger(OpCenBean.class.getName()).log(Level.SEVERE, null, ex);
            ret = false;
        }

        return ret;
    }
    
      public String getNoOrder() {
        return noOrder;
    }

    public void setNoOrder(String noOrder) {
        this.noOrder = noOrder;
    }

    public Date getDateOrder() {
        return dateOrder;
    }

    public void setDateOrder(Date dateOrder) {
        this.dateOrder = dateOrder;
    }
    
    public float totalDetails(){

        float total=0;
        
        if(allDets!=null)
        {
        for(int i = 0; i< allDets.size(); i++)
        {
            total += allDets.get(i).getPriceUnit()*allDets.get(i).getQuantity();
        }
        }
        return total ;
    }
}
